package 简单;

import java.util.HashMap;

public class 两数之和 {
    public static void main(String[] args) {
        int[] nums = {1,4,5,6,7,8,9,10};
        int[] x = twoSum(nums, 3);
        for (int i : x) {
            System.out.println(i);
        }
    }

    public static int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        HashMap<Integer,Integer> map = new HashMap<>(len-1);
        map.put(nums[0],0 );
        for (int i = 1; i < nums.length; i++) {
            if (map.containsKey(target-nums[i])){
                return new int[]{map.get(target-nums[i]),i};
            }
            map.put(nums[i],i );
        }
        return new int[]{-1,-1};
    }
}
